package cn.hdu.liang.ch3;

public class Await {
    static volatile int count;

    public static void main(String[] args) {
        Runnable r = () -> {
            Thread curThread = Thread.currentThread();
            System.out.printf("%s 等待中\n", curThread.getName());

            synchronized(Await.class) {
                count++;
                try {
                    Thread.sleep(2000);
                    while(count < 3) {
                        Await.class.wait();
                    }
                } catch(InterruptedException ie) {

                }
            }
            System.out.printf("%s 唤醒了\n", curThread.getName());
        };
        Thread thdA = new Thread(r, "线程A");
        Thread thdB = new Thread(r, "线程B");
        Thread thdC = new Thread(r, "线程C");
        thdA.start();
        thdB.start();
        thdC.start();

        r = () -> {
            try {
                while(count < 3) {
                    Thread.sleep(100);
                }
                synchronized(Await.class) {
                    Await.class.notifyAll();
                }
            } catch(InterruptedException ie) {

            }
        };
        Thread thd = new Thread(r);
        thd.start();
    }
}
